package indi.gtl.superbrain.activitys;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import indi.gtl.superbrain.R;
import indi.gtl.superbrain.models.Constant;
import indi.gtl.superbrain.models.UserInfo;
import indi.gtl.superbrain.tools.NetTool;
import indi.gtl.superbrain.tools.NetToolService;
import indi.gtl.superbrain.widget.CircleChart;

import java.lang.ref.WeakReference;


public class ServiceActivity extends Activity implements Constant {
    private static final String TAG = "ServiceActivity";

    private UserInfo userInfo;

    RelativeLayout myuserInfoServiceRelativeLayout, enemyInfoServiceRelativeLayout;
    ImageView myuserHeadServiceImageView, enemyHeadServiceImageView;
    TextView myuserNameServiceTextView, myuserScoreServiceTextView,
            enemyNameServiceTextView, enemyScoreServiceTextView,
            waitServiceTextView;
    CircleChart infoChartServiceCircleChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);


        if(getActionBar() != null)
        	getActionBar().hide();
        
        findView();

        userInfo = new UserInfo();
        userInfo.setValue("Haha", R.drawable.head_1, "990001020306700", "12345", 60, 60, 60, 60, 60, 60, 10230);

        infoChartServiceCircleChart.setTitle(UserInfo.getTitles());
        infoChartServiceCircleChart.addInfoList(userInfo.getInfoList());
        myuserHeadServiceImageView.setImageResource(userInfo.getHeadImage());
        myuserNameServiceTextView.setText(userInfo.name);
        myuserScoreServiceTextView.setText(userInfo.sum_score + "");

        startService();
    }

    private void findView(){
        myuserInfoServiceRelativeLayout = (RelativeLayout) findViewById(R.id.myuserInfoServiceRelativeLayout);
        enemyInfoServiceRelativeLayout = (RelativeLayout) findViewById(R.id.enemyInfoServiceRelativeLayout);
        myuserHeadServiceImageView = (ImageView) findViewById(R.id.myuserHeadServiceImageView);
        enemyHeadServiceImageView = (ImageView) findViewById(R.id.enemyHeadServiceImageView);
        myuserNameServiceTextView = (TextView) findViewById(R.id.myuserNameServiceTextView);
        myuserScoreServiceTextView = (TextView) findViewById(R.id.myuserScoreServiceTextView);
        enemyNameServiceTextView = (TextView) findViewById(R.id.enemyNameServiceTextView);
        enemyScoreServiceTextView = (TextView) findViewById(R.id.enemyScoreServiceTextView);
        waitServiceTextView = (TextView) findViewById(R.id.waitServiceTextView);
        infoChartServiceCircleChart = (CircleChart) findViewById(R.id.infoChartServiceCircleChart);
    }

    private void startService(){
        final NetToolService netToolService = new NetToolService(userInfo.toString());
        netToolService.setOnConnectClientListener(new NetToolService.OnConnectClientListener() {
            @Override
            public void onConnectClient(NetTool netTool) {
                // Toast.makeText(ServiceActivity.this, "被连接:" + netTool.getTheOtherIP(), Toast.LENGTH_LONG).show();
                final NetTool mNetTool = NetTool.getNetTool();
                mNetTool.setOnReceiveMessageListener(new NetTool.OnReceiveMessageListener() {
                    @Override
                    public void receiveMessage(String msg) {
                        mNetTool.sendMessage(userInfo.toString());
                        UserInfo enemyInfo = UserInfo.parse(msg);

                        enemyHeadServiceImageView.setImageResource(enemyInfo.getHeadImage());
                        enemyNameServiceTextView.setText(enemyInfo.name);
                        enemyScoreServiceTextView.setText(enemyInfo.sum_score + "");

                        infoChartServiceCircleChart.addInfoList(enemyInfo.getInfoList());

                        waitServiceTextView.setVisibility(View.GONE);
                        enemyInfoServiceRelativeLayout.setVisibility(View.VISIBLE);
                        mNetTool.setOnReceiveMessageListener(null);
                    }

                    @Override
                    public void onDisconnect() {
                        Toast.makeText(ServiceActivity.this, "断开连接", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
        netToolService.startService();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
